#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/csma-module.h"
#include "ns3/applications-module.h"
#include "ns3/bridge-helper.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("hello-simulator");

int main (int argc, char *argv[])
{
  LogComponentEnable ("hello-simulator", LOG_LEVEL_INFO);

  Time now = Simulator::Now();
  NS_LOG_INFO(now.GetSeconds() << "s:" << "hello-simulator");

  NodeContainer switches;
  switches.Create(3);
  NetDeviceContainer switchDevices;
  NetDeviceContainer switchDevices1;
  NetDeviceContainer switchDevices2;

  CsmaHelper csmaHelper;
  csmaHelper.SetChannelAttribute ("DataRate", DataRateValue (5000000));
  csmaHelper.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
  switchDevices1 = csmaHelper.Install(NodeContainer(switches.Get(0), switches.Get(1)));
  // switchDevices2 = csmaHelper.Install(NodeContainer(switches.Get(0), switches.Get(2)));

  // switchDevices.Add(switchDevices1.Get(0));
  // switchDevices.Add(switchDevices2.Get(0));
  // switchDevices.Add(switchDevices1.Get(1));

  BridgeHelper bridgetHelper;
  // bridgetHelper.Install(switches.Get(0), switchDevices);
  // bridgetHelper.Install(switches.Get(1), switchDevices);
  bridgetHelper.Install(switches.Get(0), NetDeviceContainer(switchDevices1.Get(0)));
  bridgetHelper.Install(switches.Get(1), NetDeviceContainer(switchDevices1.Get(1)));
  NodeContainer terminals;
  terminals.Create(4);

  NetDeviceContainer terminalDevices;
  NetDeviceContainer terminalSwitchDevices1;
  NetDeviceContainer terminalSwitchDevices2;
  for(int i = 0;i < 2;i++) {
    NetDeviceContainer links = csmaHelper.Install(NodeContainer(terminals.Get(i), switches.Get(1)));
    terminalDevices.Add(links.Get(0));
    terminalSwitchDevices1.Add(links.Get(1));
  }
  for(int i = 2;i < 4;i++) {
    NetDeviceContainer links = csmaHelper.Install(NodeContainer(terminals.Get(i), switches.Get(2)));
    terminalDevices.Add(links.Get(0));
    terminalSwitchDevices2.Add(links.Get(1));
  }

  bridgetHelper.Install(switches.Get(1), terminalSwitchDevices1);
  bridgetHelper.Install(switches.Get(2), terminalSwitchDevices2);

  InternetStackHelper internet;
  internet.Install (terminals);
  internet.Install (switches);

  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  ipv4.Assign (terminalDevices);

  uint16_t port = 9;
  OnOffHelper onoff ("ns3::UdpSocketFactory", 
                     Address (InetSocketAddress (Ipv4Address ("10.1.1.3"), port)));
  onoff.SetConstantRate (DataRate ("100kb/s"));
  ApplicationContainer app = onoff.Install (terminals.Get (0));
  app.Start (Seconds (1.0));
  app.Stop (Seconds (10.0));

  PacketSinkHelper sink ("ns3::UdpSocketFactory",
                         Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
  app = sink.Install (terminals.Get (2));
  app.Start (Seconds (0.0));

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  // csmaHelper.EnablePcapAll ("csma-bridge", true);
  // csmaHelper.EnablePcap("csma-bridge", terminals, true);
  csmaHelper.EnablePcap("csma-bridge", switches, true);
  Simulator::Run ();
  Simulator::Destroy ();
  NS_LOG_INFO ("Done.");

  return 0;
}
